package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.BrandService;
import com.atguigu.spzx.model.pojo.product.Brand;
import com.atguigu.spzx.model.vo.common.Result;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/product/brand")
@Tag(name = "品牌管理")
@CrossOrigin(allowCredentials = "true", originPatterns = "*", allowedHeaders = "*")
public class BrandController {
    @Autowired
    private BrandService brandService;

    @GetMapping("/findPage/{pageNum}/{pageSize}")
    @Operation(summary = "分页查询")
    public Result<Page<Brand>> findPage(@PathVariable("pageNum")Integer pageNum,
                                        @PathVariable("pageSize")Integer pageSize,
                                        @RequestParam("brandName") String brandName){
        Page<Brand> page = brandService.findPage(pageNum, pageSize, brandName);
        return Result.ok(page);
    }

    @PutMapping("/updateBrand")
    @Operation(summary = "修改品牌")
    public Result updateBrand(@RequestBody Brand brand){
        brandService.updateById(brand);
        return Result.ok();
    }

    @PostMapping("/insertBrand")
    @Operation(summary = "添加品牌")
    public Result insertBrand(@RequestBody Brand brand){
        brandService.save(brand);
        return Result.ok();
    }

    @DeleteMapping("/deleteBrandById/{id}")
    @Operation(summary = "添加品牌")
    public Result insertBrand(@PathVariable("id")Long id){
        brandService.removeById(id);
        return Result.ok();
    }

    @GetMapping("/findAll")
    @Operation(summary = "获取所有品牌信息")
    public Result<List<Brand>> findAll(){
        return Result.ok(brandService.list());
    }
}
